package com.iampzh.oa.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iampzh.oa.dao.DepartmentDaoImpl;
import com.iampzh.oa.dao.PostDaoImpl;
import com.iampzh.oa.dao.UserDaoImpl;
import com.iampzh.oa.entitys.Department;
import com.iampzh.oa.entitys.Post;
import com.iampzh.oa.entitys.User;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserDaoImpl userDaoImpl;
	@Autowired
	private DepartmentDaoImpl departmentDaoImpl;
	@Autowired
	private PostDaoImpl postDaoImpl;
	/**
	 * 显示所有用户列表
	 * @param model
	 * @return
	 */
	@RequestMapping("list")
public String listAll(Model model){
		List<User> allUser = userDaoImpl.getAllUser();
		model.addAttribute("listall", allUser);
	return  "user/list";}
	/**
	 * 跳转到新建用户界面
	 * @return
	 */
	@RequestMapping("userAction_addUI")
	public String addUI(Model model){
		List<Department> allDepartment = departmentDaoImpl.getAllDepartment();
		List<Post> allPost = postDaoImpl.getAllPost();
		model.addAttribute("departments", allDepartment);
		model.addAttribute("posts", allPost);
		return "user/addUI";}
	/**
	 * 保存用户信息，既新建用户
	 * @return
	 */
	@PostMapping("userAction")
	public String saveUser(HttpServletRequest request){
		String departmentId = request.getParameter("departmentId");
		String loginName = request.getParameter("loginName");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String phoneNumber = request.getParameter("phoneNumber");
		String email = request.getParameter("email");
		String description = request.getParameter("description");
		String post = request.getParameter("post");
		User user=new User();
		user.setDepartment(departmentDaoImpl.getDepartment(Integer.parseInt(departmentId)));
		user.setLoginname(loginName);
		user.setUsername(name);
		user.setSex(sex=="male"?"女":"男");
		HashSet<Post> posts=new HashSet<>();
		posts.add(postDaoImpl.getPost(Integer.parseInt(post)));
		user.setPhone(phoneNumber);
		user.setEmail(email);
		user.setUdesc(description);
		user.setPosts(posts);
		userDaoImpl.save(user);
		return "redirect:list";}
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping("userAction_delete/{id}")
	public String deleteUser(@PathVariable("id")int id){
		userDaoImpl.delete(id);
		return "redirect:../list";
		
	}
	@RequestMapping("userAction_editUI/{id}")
	public String editUI(@PathVariable("id") int id,Model model){
		User user = userDaoImpl.getUser(id);
		Department department = user.getDepartment();
		Set<Post> posts = user.getPosts();
		model.addAttribute("user", user);
		model.addAttribute("department", department);
		model.addAttribute("userpost", posts.iterator().next());
		List<Department> allDepartment = departmentDaoImpl.getAllDepartment();
		List<Post> allPost = postDaoImpl.getAllPost();
		model.addAttribute("departments", allDepartment);
		model.addAttribute("posts", allPost);
		return "/user/editUI";}
}
